/**
 * gamelet
 *
 * outlook@China software studio
 * Copyright (c) 2008-2011. All Rights Reserved.
 * http://www.outlook.com
 */

package org.outlook.gamelet.role.tank1990;

import org.outlook.common.data.ListMap;
import org.outlook.common.measure.Direction;
import org.outlook.jgamebox.map.Scene;
import org.outlook.jgamebox.role.Spriter;
import org.outlook.jgamebox.role.fighter.Weapon;

/**
 * function:
 * @author watson  email: watson.wu@hotmail.com
 * @date 2011-5-30
 */
public class Missile extends Weapon {

	private static final long serialVersionUID = 2452759492545157039L;
	private Direction direction;

	public Missile(Spriter from) {
		super(from);
	}
	public Missile(Spriter from, final Direction dire) {
		super(from);
		this.direction = dire;
		setMoveProbability(0);
		setChangeDirectionProbability(100);
	}

	@Override
	protected Object clone() throws CloneNotSupportedException {
		return super.clone();
	}
	
	@Override
	public void run() {
		while(isAlive()) {
			//TODO
			if(isBeyondMap() || isHit()) {//if cross edge or hit anything, die
				setLife(0);
			} else
				super.move(getDirection());
		}
		super.destory();
	}

	public boolean isHit() {
		final ListMap<Spriter, Object> spriters = getScene().getSpriters();
		boolean heroMissile = getFrom() instanceof TankHero;
		for(Spriter sp : spriters.getKeys()) {
			if(sp.equals(getFrom()) || (heroMissile && sp instanceof TankHero) || (!heroMissile && !(sp instanceof TankEnemy))) {
				continue;
			} else {
				if(Scene.isHit(this, sp))
					return true;
			}
		}
		return false;
	}

	public Direction getDirection() {
		return direction;
	}

	@Override
	@Deprecated
	public void setDirection(Direction direction) {
	}
}
